package com.qd.panda.service.stroke.delay;

import com.cdqidi.util.SpringContextHolder;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.panda.model.UserStroke;
import com.qd.panda.service.AsyncService;
import com.qd.panda.service.stroke.DisplayUtil;
import com.qd.panda.service.stroke.UserStrokeConfirmService;
import com.qd.panda.service.stroke.UserStrokeHolderService;
import com.qd.panda.service.stroke.UserStrokeService;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

/**
 * 目前已经废弃，使用redisson方案
 * @author sjk
 */
@Data
@ToString
@Slf4j
public class UserStrokeDelay implements Runnable {
    /**
     * 行程ID
     */
    private String sid;
    /**
     * 订单ID
     */
    private String orderId;

    public UserStrokeDelay(String sid, String orderId) {
        this.sid = sid;
        this.orderId = orderId;
    }

    @Override
    public void run() {
        LocalDateTime now = LocalDateTime.now();
        boolean isClear = false;
        UserStrokeService userStrokeService = SpringContextHolder.getBean(UserStrokeService.class);
        UserStroke userStroke = userStrokeService.getById(sid);
        if (null == userStroke) {
            //行程不存在的时候不处理
            return;
        }
        if (userStroke.getProcessStatus().equals(DisplayUtil.PROCESS_STATUS_0)) {
            final boolean check = SpringContextHolder.getBean(UserStrokeConfirmService.class).doorCheck(userStroke.getParkId(), userStroke.getSid(), PandaConstant.DIRECTION_1);
            log.info("parkId: {},sid:{},check: {}", userStroke.getParkId(), userStroke.getSid(), check);
            if (check) {
                //停车场设置了需要人工确认，并且用户已经扫码了，此时为true。不做处理
                return;
            }
            //只有未入场的时候才需要清理,并且发送通知消息
            UserStrokeHolderService userStrokeHolderService = SpringContextHolder.getBean(UserStrokeHolderService.class);
            userStrokeHolderService.cancelReserve(sid, orderId, DisplayUtil.PROCESS_STATUS_2, false, "超时程序自动清理");
            isClear = true;
        }
        if (isClear) {
            final AsyncService asyncService = SpringContextHolder.getBean(AsyncService.class);
            asyncService.createNoticeMsg(userStroke, now);
        }
        log.info("开始执行超时取消预约订单操作,行程id：{}，订单ID： {},isClear: {}", sid, orderId, isClear);
    }
}
